﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using PreeRegisterModel;
using System.Collections.Specialized;
using PreeRegisterBLL;
using PreeRegisterBLL.Security;

namespace PreeRegister_WebAPI.Controllers
{
    public class labController : ApiController
    {
        [HttpPost]
        /// <summary>
        /// 查询检验报告列表
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel report(NameValueCollection any)
        {
            try
            {

                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);
                ReportDetailResponseModel reportDetailResponseModel = new ReportDetailResponseModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";

                LabReportListResponseModel labReportListResponseModel = new LabReportListResponseModel();
                
                if (!ResultDecode.DecodeResult(jsonData, "lab|report", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.LabReportListResponseModel(labReportListResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("lab|report|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                LabReportListRequestModel labReportListRequestModel = csSerialize.LabReportListRequestModel(body_json);

                //查询检验报告列表
                labReportListResponseModel = LabReportBLL.GetLabReportListNew2(labReportListRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.LabReportListResponseModel(labReportListResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("lab|report|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                LabReportListResponseModel labReportListResponseModel = new LabReportListResponseModel();
                labReportListResponseModel.code = "-1000";
                labReportListResponseModel.msg = "获取检验报告异常2：" + err.Message;

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.LabReportListResponseModel(labReportListResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("lab|report|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        /// <summary>
        /// 查询检验报告明细
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel detail(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);
                ReportDetailResponseModel reportDetailResponseModel = new ReportDetailResponseModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";

                LabReportDetailResponseModel labReportDetailResponseModel = new LabReportDetailResponseModel();

                if (!ResultDecode.DecodeResult(jsonData, "lab|detail", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.LabReportDetailResponseModel(labReportDetailResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("lab|detail|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                LabReportDetailRequestModel labReportDetailRequestModel = csSerialize.LabReportDetailRequestModel(body_json);

                //查询检验报告列表
                labReportDetailResponseModel = LabReportBLL.GetLabReportDetailNew2(labReportDetailRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.LabReportDetailResponseModel(labReportDetailResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("lab|detail|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                LabReportDetailResponseModel labReportDetailResponseModel = new LabReportDetailResponseModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.LabReportDetailResponseModel(labReportDetailResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("lab|detail|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }
    }
}
